const vscode = require('vscode')
const ztmz_tokens = require('./tokens_provider');
const attributes = ztmz_tokens.attributes;
const tokenMap = ztmz_tokens.tokenMap;
const tokenDict = ztmz_tokens.tokenDict;

let attributesCompletionItems = [];
attributes.forEach(attribute => {
    let item = new vscode.CompletionItem(attribute.name, vscode.CompletionItemKind.Reference);
    item.detail = attribute.detail;
    item.documentation = attribute.description;
    attributesCompletionItems.push(item);
});
    

let tokensCompletionItems = [];
for (let filename in tokenDict) {
    let token = tokenDict[filename];
    let item = new vscode.CompletionItem(filename, tokenMap[token.type]);
    item.detail = token.type;
    item.documentation = token.markdown;
    tokensCompletionItems.push(item);
}

async function getCompletionProvider() {
    return vscode.languages.registerCompletionItemProvider({ language: 'pacenote', scheme: 'file' }, {
        provideCompletionItems(document, position) {
            const linePrefix = document.lineAt(position).text.slice(0, position.character);
            if (linePrefix.endsWith('@')) {
                // return attribute completion
                return attributesCompletionItems;
            } else if (linePrefix.endsWith(':') || linePrefix.endsWith(',') || linePrefix.endsWith('/')) {
                // return token completion
                return tokensCompletionItems;
            } else {
                // return token completion
                return undefined;
            }
        }
    }, ':', ',', '/', '@');
}

module.exports = { 
    getCompletionProvider
}
